package com.spzx.manager.task;

import cn.hutool.core.date.DateUtil;
import com.spzx.manager.mapper.OrderInfoMapper;
import com.spzx.manager.mapper.OrderStatisticsMapper;
import com.spzx.model.entity.order.OrderStatistics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 订单定时任务
 * Project: spzx-parent
 * Package: com.spzx.manager.task
 * Version: 1.0
 * <p>
 * Created by  wangjiaxin  on 2024/3/6 22:27
 */
@Component
@Slf4j
public class OrderStatisticsTask {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    /**
     * 每天凌晨2点定时运行订单金额统计
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderTotalAmountStatistics() {
        //获取前一天日期
        String createTime = DateUtil.offsetDay(new Date(), -1).toString(new SimpleDateFormat("yyyy-MM-dd"));
        //统计前一天金额
        OrderStatistics orderStatistics = orderInfoMapper.selectOrderStatistics(createTime);
        if (orderStatistics != null) {
            //写入到结果数据库中
            orderStatisticsMapper.insert(orderStatistics);
        }
    }
}
